/**
 * 非对称性加解密工具函数
 */
const crypto = require("crypto");
const NodeRSA = require("node-rsa");
const { KJUR, hextob64, b64tohex } = require("jsrsasign");
const iv = Buffer.from([
  0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe,
  0xf,
]);
module.exports = {
  // 生成N位字符串的AES密钥
  createAesKey(randomFlag, min, max) {
    let str = "",
      range = min,
      arr = [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
        "e",
        "f",
        "g",
        "h",
        "i",
        "j",
        "k",
        "l",
        "m",
        "n",
        "o",
        "p",
        "q",
        "r",
        "s",
        "t",
        "u",
        "v",
        "w",
        "x",
        "y",
        "z",
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z",
      ];

    // 随机产生
    if (randomFlag) {
      range = Math.round(Math.random() * (max - min)) + min;
    }
    for (let i = 0; i < range; i++) {
      pos = Math.round(Math.random() * (arr.length - 1));
      str += arr[pos];
    }
    return str;
  },

  // AES加密
  aesEncryption(data, key) {
    const cipher = crypto.createCipheriv("aes-128-cfb", key, iv);
    let encryptedData = cipher.update(data, "utf-8", "base64");
    encryptedData += cipher.final("base64");
    return encryptedData;
  },
  // aed解密
  aesDecrypt(data, key) {
    let decipher = crypto.createDecipheriv("aes-128-cfb", key, iv);
    let decoded = decipher.update(data, "base64", "utf8");
    decoded += decipher.final("utf8");
    return decoded;
  },
  // RSA加密
  rsaEncrypt(data) {
    const publicKey = new NodeRSA(this.app.config.xhConfig.rsaPk);
    publicKey.setOptions({ encryptionScheme: "pkcs1" }); //指定加密格式
    return publicKey.encrypt(data, "base64", "utf8");
  },

  // RSA解密
  rsaDecrypt(data) {
    const publicKey = new NodeRSA(this.app.config.companyConfig.rasPEk);
    publicKey.setOptions({ encryptionScheme: "pkcs1" }); //指定加密格式
    let baseData = publicKey.decrypt(data, "base64", "utf8");
    return Buffer.from(baseData, "base64").toString();
  },
  // rsa加签
  rsaSign(content) {
    const signature = new KJUR.crypto.Signature({
      alg: "SHA256withRSA",
      //!这里指定 私钥 pem!
      prvkeypem: this.app.config.companyConfig.rasPEk,
    });
    signature.updateString(content);
    const signData = signature.sign();
    // 将内容转成base64
    return hextob64(signData);
  },

  // rsa验签
  rsaVerify(content, sign) {
    let signatureVf = new KJUR.crypto.Signature({
      alg: "SHA256withRSA",
      prvkeypem: this.app.config.xhConfig.rsaPk,
    });
    signatureVf.updateString(content);
    // 验签入参是16进制字符串，注意转码
    let b = signatureVf.verify(b64tohex(sign));
    console.log(`公钥验签：${b}`);
  },
};
